/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: client.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月13日 星期2 16时18分54秒
***************************************************/
// 开发一个计算服务器，能够计算两个整数的加减乘除
//          ##add#23#12##

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct head
{
	int type;
	char mean[10];
};


struct data
{
	float num1;
	float num2;
};
struct number
{
	float number;
};
int main(int argc,const char* argv[])
{
	int sock = 0;
	int ret = 0;
	char buf[1024] = {0};
	char dest[1024] = {0};
	char msg[50] = {0};
	int i = 0;
	
	// 初始化结构体变量用于保存服务器地址和信息
	struct sockaddr_in serAddr;
	memset(&serAddr,0,sizeof(serAddr));

	// 创建UDP套接字 UDP标识  传输模式(报文传输)
	sock = socket(AF_INET,SOCK_DGRAM,0);
	if(sock == -1) {
		perror("socket");
		exit(-1);
	}

	// 设置地址族
	serAddr.sin_family = AF_INET;
	// 设置端口号
	serAddr.sin_port = htons(6666);
	// 设置ip
	serAddr.sin_addr.s_addr = inet_addr("192.168.198.128");
	while(1){
	struct head hd;
	memset(&hd,0,sizeof(hd));
	
	struct data dat;
	memset(&dat,0,sizeof(dat));
	printf("你想进行什么操作");
	scanf("%d",&i);
	if(i == 0){
		memcpy(msg,&hd,sizeof(struct head));
		ret = sendto(sock,msg,sizeof(struct head),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
		break;
	}
	hd.type = i;
	strcpy(hd.mean,"add");
	printf("请输入数字1:\n");
	scanf("%f",&dat.num1);
	printf("请输入数字2:\n");
	scanf("%f",&dat.num2);

	
	memcpy(msg,&hd,sizeof(struct head));
	memcpy(msg + sizeof(struct head),&dat,sizeof(struct data));

    	ret = sendto(sock,msg,sizeof(struct head) + sizeof(struct data),0,(struct sockaddr *)&serAddr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("sendto");
	}



	ret = recvfrom(sock,msg,sizeof(struct head) + sizeof(struct number),0,NULL,NULL);
	if(ret == -1){
		perror("recvfrom");
		exit(-1);
	}
	struct number *point = NULL;
	
	point = (struct number *)(msg + sizeof(struct head));
	printf("number = %.2f\n",point->number);
	
		}

	//关闭套接字 
	close(sock);
    return 0;
}

